package kosta.controller;

import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import kosta.model.member.MemberVO;
import kosta.model.memo.MemoService;
import kosta.model.memo.MemoVO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MemoController {
	@Resource(name="memoServiceImpl")
	private MemoService memoService;
	
	@RequestMapping("/sendMemoView.do")
	public String sendMemoView(){
		return "sendMemoView.memo";
	}
	
	@RequestMapping("/sendMemo.do")
	public String sendMemo(MemoVO memoVO) throws Exception{
		System.out.println(memoVO + "----------- memoVO");
		memoService.sendMemo(memoVO);
		return "memoResult.memo";
	}
	
	// 아이디 체크 
		@RequestMapping("/idCheck2.do")
		@ResponseBody	
		public HashMap<String, String> idCheck(String id) throws Exception{
			MemberVO vo = memoService.idCheck(id);
			String str="없는 회원.";
			if(vo!=null){
				str="";
			}
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("str", str);
			return map;
		}
		
		@RequestMapping("/getMemoList.do")
		public ModelAndView getMemoList(String id) throws Exception{
			System.out.println(id+"----------- id getMemoList");
			List<MemoVO> memoList = memoService.getMemoList(id);
			System.out.println("memoList ------: " +memoList);
			return new ModelAndView("getMemoList.memo", "memoList", memoList);
		}
		
		@RequestMapping("/showMemo.do")
		public ModelAndView showMemo(int no) throws Exception{
			memoService.showMemoCheck(no);
			return new ModelAndView("showMemo.memo");
		}
		
		@RequestMapping("/deleteMemo.do")
		public ModelAndView deleteMemo(HttpSession session, String[] memo) throws NumberFormatException, Exception{
			MemberVO mvo = (MemberVO) session.getAttribute("mvo");
			memoService.outMemo(memo);
			
			return new ModelAndView("redirect:getMemoList.do?id="+mvo.getId());
		}
		
		@RequestMapping("/sendMemoList.do")
		public ModelAndView sendMemoList(String id) throws Exception{
			System.out.println(id+"----------- id getMemoList");
			List<MemoVO> memoList = memoService.sendMemoList(id);
			return new ModelAndView("sendMemoList.memo", "memoList", memoList);
		}
}
